<?php

class Share extends AppModel {

    var $alias = 'Share';
    var $name = 'Share';

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function delete($id = null, $cascade = true) {
        App::import('Component', 'Upload');
        $this->id = $id;
        $upload = new UploadComponent();

        //delete thumbnail
        $thumbnail = $this->field('images_small');
        if (is_file(Configure::read('content_image_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('content_image_absolute_path') . $thumbnail);
        }

        parent::delete($id, $cascade);
    }

    function cacheLastestShare($limit = 7) {
        $cacheName = 'lastest_share';
        $data = $this->find('all', array(
                    'conditions' => array('Share.published' => '1'),
                    'fields' => 'Share.id, Share.title, Share.alias',
                    'limit' => $limit,
                    'order' => array(
                        'Share.id' => 'DESC'
                    ),
                ));
        Cache::write($cacheName, $data, 'share');
    }

}

?>
